package com.zb.jsp.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zb.jsp.service.LoginService;
import com.zb.jsp.utils.StringUtil;
import com.zb.jsp.vo.UserVo;

public class UserLoginServlet extends HttpServlet {
	public static String captcha;
	public static String captchaSession;
	public UserLoginServlet() {
		super();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("进入了post");
		doPost(request,response);

	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("进入了dopost");
		//实例化LonginService对象
		LoginService loginService = new LoginService();
		UserVo uservo = new UserVo();
		//从login.jsp页面获取用户名和密码
		String userName = request.getParameter("userName");
		String userPw = request.getParameter("passWd");
		
		//保存页面获取的用户名和密码
		//request.setAttribute("userName", userName);
		
		//调用loginservice方法验证数据库信息
		Map map = loginService.login(userName, userPw,0);		
		Object message = map.get("message");
		System.out.println(uservo.getUserNameCn());
		if(StringUtil.isNullOrEmpty(message)){
			//验证成功
			request.getSession().setAttribute("nameCn",map.get("nameCn"));
			request.getRequestDispatcher("/jsp/admin_index.jsp").forward(request, response);
		}else{
			//登陆失败
			request.setAttribute("message",message );
			response.sendRedirect(request.getContextPath()+"/index.jsp");
//			request.getRequestDispatcher("/index.jsp").forward(request, response);
		}
	}

}
